package com.example.baseweb.controller;

import com.example.baseweb.common.ApiResponse;
import com.example.baseweb.entity.Category;
import com.example.baseweb.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/category")
public class CategoryController {
    
    @Autowired
    private CategoryService categoryService;
    
    // 获取所有分类
    @PostMapping("/list")
    public ApiResponse<List<Category>> getAllCategories(@RequestBody(required = false) Map<String, Object> params) {
        return ApiResponse.success(categoryService.getAllCategories());
    }
    
    // 根据ID获取分类
    @PostMapping("/detail")
    public ApiResponse<Category> getCategoryById(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        return ApiResponse.success(categoryService.getCategoryById(id));
    }
    
    // 保存分类
    @PostMapping("/save")
    public ApiResponse<Category> saveCategory(@RequestBody Category category) {
        return ApiResponse.success(categoryService.saveCategory(category));
    }
    
    // 删除分类
    @PostMapping("/delete")
    public ApiResponse<Void> deleteCategory(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        categoryService.deleteCategory(id);
        return ApiResponse.success();
    }
    
    // 更新分类状态
    @PostMapping("/updateStatus")
    public ApiResponse<Category> updateCategoryStatus(@RequestBody Map<String, Object> params) {
        Long id = Long.valueOf(params.get("id").toString());
        Integer status = Integer.valueOf(params.get("status").toString());
        return ApiResponse.success(categoryService.updateCategoryStatus(id, status));
    }
    
    // 根据状态查询分类
    @PostMapping("/listByStatus")
    public ApiResponse<List<Category>> getCategoriesByStatus(@RequestBody Map<String, Object> params) {
        Integer status = Integer.valueOf(params.get("status").toString());
        return ApiResponse.success(categoryService.getCategoriesByStatus(status));
    }
} 